package com.jingfang.cloud;

import cn.hutool.core.io.FileUtil;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.themes.FlatMacLightLaf;
import com.jingfang.cloud.constant.MzConstants;
import com.jingfang.cloud.widget.check.CheckWidget;
import org.dromara.quickswing.excutor.QSTaskExecutor;
import org.dromara.quickswing.ui.app.QSContext;
import org.dromara.quickswing.ui.app.QSWidget;

import javax.swing.*;
import java.io.File;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;

public class MzContext extends QSContext<QSWidget<MzPrefs>, MzPrefs> {
    public final static QSTaskExecutor TASK_EXECUTOR = new QSTaskExecutor(Executors.newCachedThreadPool());
    @Override
    protected QSWidget<MzPrefs> createApplication(String[] strings, MzPrefs mzPrefs) {
        FlatLaf.registerCustomDefaultsSource(MzConstants.APP_THEME_PACKAGE);
        ToolTipManager.sharedInstance().setInitialDelay(5);
        ToolTipManager.sharedInstance().setLightWeightPopupEnabled(true);
        FlatLaf.setUseNativeWindowDecorations(true);
        String javaVersion = System.getProperty("java.version");
        if (javaVersion.startsWith("1.8")) {
            System.setProperty("flatlaf.uiScale", "1x");
        }
        FlatMacLightLaf.setup();
        return new CheckWidget(this, MzConstants.APP_NAME, mzPrefs);
    }

    @Override
    protected void populatePreferencesFromApplication(QSWidget<MzPrefs> app, MzPrefs preferences) {

    }

    @Override
    protected MzPrefs loadPreferences(String[] strings) {
        return new MzPrefs();
    }

    @Override
    protected String getAppDataPath() {
        return FileUtil.getUserHomePath() + File.separator + "." + MzConstants.APP_DATA_DIR.toLowerCase();
    }

    @Override
    protected String getAppResourceBundlePath() {
        return "";
    }

    @Override
    public <D, R> Future<R> taskSubmit(Callable<D> callable, BiFunction<D, Exception, R> biFunction) {
        return TASK_EXECUTOR.submit(callable, biFunction);
    }

    @Override
    public <D> void taskExecute(Callable<D> callable, BiConsumer<D, Exception> biConsumer) {
        TASK_EXECUTOR.execute(callable, biConsumer);
    }
}
